require( "renderx" )
EFFECT.clipplanes = {}

function EFFECT:Init(data)
	self.Entity:SetParent(data:GetEntity())
	self.heirach = data:GetEntity()
	self.mat = Material(self.heirach:GetNetworkedString("domematerial"))
	self.Entity:SetModel("models/zup/shields/200_shield.mdl")
	self.Radius = self.heirach:GetNetworkedInt("Radius")
	self.Entity:SetColor(255,255,255,70)
	local ModelSize = (self.Entity:OBBMaxs() - self.Entity:OBBCenter()).z
	self.ModelScale = self.Radius/ModelSize
	self.Entity:SetPos((data:GetEntity():GetPos() - self.Entity:LocalToWorld(self.Entity:OBBCenter() * self.ModelScale)) + data:GetEntity():GetPos())
	
	local boundlimit = self.Radius*Vector()
	self.Entity:SetRenderBounds(-2*boundlimit,2*boundlimit)
	
	self.clipplanes = {}
end

function EFFECT:Think()
	if self.heirach:IsValid() then
		self.clipplanes = self.heirach.ClipPlanes
		return true
	else
		return false
	end
end 

function EFFECT:Render()
		local size = self.ModelScale
		local clip = true --util.tobool(GetConVar("cl_cdamage_shieldclippingenabled"))
		SetMaterialOverride(self.mat)
		self.Entity:SetModelScale(Vector()*size)
		local planes = 0
		if (clip) then
			render.EnableClipping(true)
			for _,plane in pairs(self.clipplanes) do
				render.PushCustomClipPlane( plane.Normal, plane.Distance)
				planes = planes + 1
			end
		end
		self.Entity:DrawModel()
		for remover=0, planes do
			render.PopCustomClipPlane()
		end
		render.EnableClipping(false)
		SetMaterialOverride(nil)
end
